package com.pla.clipboardimagepublisher;

import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.wonderly.awt.Packer;

public class AboutDialog extends JDialog {
  private static final long serialVersionUID = 6155171161010663184L;
  private static ClassLoader classLoader = AboutDialog.class.getClassLoader();

  public AboutDialog(JFrame owner) {
    super(owner, true);
    Properties properties = new Properties();
    try {
      properties.load(classLoader.getResourceAsStream("resources/version.properties"));
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    setTitle("About");
    JPanel panel = new JPanel();
    Packer packer = new Packer(panel);
    JLabel label = new JLabel();
    label.setIcon(ClipboardImagePublisherGUI.getIcon("clipboard_next48x48.png"));
    int y = 0;
    packer.add(label).gridx(0).gridy(y++).gridw(2);
    packer.add(new JLabel("Title")).gridx(0).gridy(y).west().gridw(1).inset(2, 2, 2, 2);
    packer.add(new JLabel("Clipboard Image Publisher GUI")).gridx(1).gridy(y++);
    packer.add(new JLabel("Author")).gridx(0).gridy(y).west().gridw(1);
    packer.add(new JLabel("Patrick L Archibald")).gridx(1).gridy(y++);
    packer.add(new JLabel("Version")).gridx(0).gridy(y);
    packer.add(
        new JLabel(properties.getProperty("version.major")
            + "."
            + properties.getProperty("version.minor"))).gridx(1).gridy(y++);
    packer.add(new JLabel("Build date")).gridx(0).gridy(y);
    packer.add(new JLabel(properties.getProperty("version.date"))).gridx(1).gridy(y++);
    packer.add(new JLabel("Java version")).gridx(0).gridy(y);
    packer.add(new JLabel(System.getProperty("java.version"))).gridx(1).gridy(y++);
    JButton okButton = new JButton("OK");
    okButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        setVisible(false);
        dispose();
      }
    });
    packer.add(okButton).gridw(2).gridx(0).gridy(y++).anchor(GridBagConstraints.CENTER);
    getRootPane().setDefaultButton(okButton);
    okButton.requestFocus();
    setContentPane(panel);
    pack();
    setLocationRelativeTo(owner);
    setVisible(true);
  }
}
